﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;
using System.Data;

namespace Dryricenet
{
    public partial class ph_Update : System.Web.UI.Page
    {
        public string tx = "";
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            id = Convert.ToInt32(dr["UserID"]);
            if (dr["Image"].ToString() == "")
            {
                tx = "img/profile_small.jpg";
            }
            else
            {
                tx = dr["Image"].ToString();
            }
        }
        //上传
        protected void Button2_Click(object sender, EventArgs e)
        {
            User u = new User();

            u.UserID = id;
            if (FileUpload1.HasFile)
            {
                if (CheckFileType(FileUpload1.FileName))
                {
                    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Path.GetExtension(FileUpload1.FileName);
                    string filePath = "img/" + fileName;
                    FileUpload1.SaveAs(MapPath(filePath));
                    u.Image = filePath;
                }
            }
            if (FileUpload1.FileName!="")
            {
                if (userBLL.updateImageBLL(u))
                {
                    Response.Write("<script>javascript:alert('上传成功!');history.back();</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>javascript:alert('上传失败!');history.back();</script>");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('您还没有上传图片!');history.back();</script>");
                Response.End();
            }

        }
        public bool CheckFileType(string filename)
        {
            string ext = Path.GetExtension(filename);
            switch (ext.ToLower())
            {
                case ".gif": return true;
                case ".png": return true;
                case ".jpg": return true;
                case ".jpeg": return true;
                default: return false;
            }
        }
    }
}